<?php

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;

class LoginRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
           "username"=>"required",
           "password"=>"required",
           "code"=>"required|captcha",
        ];
    }

    public function messages()
    {
        return [
            'username.required'=>"用户名不能为空",
            'password.required'=>"密码不能为空",
            'code.required'=>"验证码不能为空",
            'code.captcha'=>'验证码不正确'
        ];
    }

    // 重写父类方法failedValidation
    public function failedValidation(Validator $validator)
    {
        $error = $validator->errors()->all();
        throw new HttpResponseException(response()->json(['code' => 400, 'message' => $error[0]]));
//        throw new \HttpResponseException(response()->json(['code'=>400,'message'=>$error[0]]));
    }

}
